const axios = require('axios');
const {randStr} = require("../utils/randUtil");
const {sign} = require("../utils/signUtil");
const qs = require('qs');

const login = (username, password) => {
    const rs = randStr(16);
    const data = {
        gameid: 10,
        deviceType: 'android',
        password: password,
        agent: 'app-10-0221',
        logintime: new Date().getTime(),
        tfyuuid: rs,
        anid: rs,
        appid: 1001,
        username: username,
        imei: '',
        oaid: ''
    }
    data.sign = sign(data);
    return new Promise((resolve, reject) => {
        axios({
            method: 'POST',
            url: 'https://xfsdk.maple-game.com/app/login.php',
            data: qs.stringify(data),
            headers: {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err);
        })
    })
}

module.exports = {
    login
}